<?php

namespace App\Admin\Controllers;

use App\Models\BookListCategory;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class BookListCategoriesController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('书单分类列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改分类');

            $content->body($this->form()->edit($id));
        });
    }

    public function update($id)
    {
        return $this->form()->update($id);
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加分类');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(BookListCategory::class, function (Grid $grid) {
            // 默认按创建时间倒序排序
            $grid->model()->orderBy('created_at', 'desc');
            $grid->model()->where('deleted',0);

            //$grid->id('ID')->sortable();
            $grid->title('分类名称');

            $grid->disablePagination();
            $grid->disableExport();
            $grid->disableFilter();
            $grid->disableRowSelector();

            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            //$grid->created_at();
            //$grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(BookListCategory::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('title', '分类名称')->rules('required');
            //$form->display('created_at', 'Created At');
            //$form->display('updated_at', 'Updated At');
            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });
        });
    }

    public function destroy($id)
    {
        //$this->form()->destroy()
        BookListCategory::where('id',$id)->update(['deleted'=>1]);
        return ['status'=>true,'message'=>'删除成功'];
    }
}
